<?php

namespace app\common\model;

use think\Model;

class UserRule extends Model
{

    // 表名
    protected $name = 'user_rule';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];

    public function childs()
    {
        return $this->hasMany(UserRule::class, 'pid', 'id');
    }

    //获取列表
    public static function getLists()
    {
        return self::with('childs')->where(['status'=>'normal','ismenu'=>1,'pid'=>0])->order('weigh','desc')->select();
    }

    //获取对应的子元素ids
    public static function getChildsIds($pid)
    {
        return self::where(['status'=>'normal','pid'=>$pid])->column('id');
    }

    //获取审批子元素
    public static function getApproves($pid)
    {
        return self::with('approves')->where(['pid'=>$pid])->order('weigh','desc')->field('id,title,weigh')->select();
    }
}
